package com.starlink.controller.wechat;

import com.starlink.common.PageInfoTwo;
import com.starlink.common.WebResponse;
import com.starlink.common.annotation.WebAccess;
import com.starlink.model.request.ShoppingCartDetailRequest;
import com.starlink.model.vo.ShoppingCartDetailVO;
import com.starlink.service.ShoppingCartDetailService;
import com.starlink.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * @Auther: zhouyuhang01
 * @Date: 2024/4/8 10:06
 * @Description: WechatMallCarController
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/wechat/mallcar")
@Api(value = "【小程序端】商城购物车模块", tags = {"【小程序端】商城购物车模块"})
public class WechatMallCarController {
    @Autowired
    private ShoppingCartDetailService cartDetailService;

    @WebAccess(login = true)
    @ApiOperation(value = "根据用户ID查询购物车商品数量及ID", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/get/{userId}")
    public WebResponse<Map<String,Long>> getByUserId(@PathVariable("userId") long userId) {
        return WebResponse.build(cartDetailService.getByUserId(userId));
    }

    @WebAccess(login = true)
    @ApiOperation(value = "购物车分页查询")
    @PostMapping("/pageQuery")
    public WebResponse<PageInfoTwo<ShoppingCartDetailVO>> pageQuery(@RequestBody ShoppingCartDetailRequest request) {
        return WebResponse.build(cartDetailService.pageQuery(request));
    }

    @WebAccess(login = true)
    @ApiOperation("初始化购物车并加入购物车——从商品详情处加车")
    @PostMapping("/addCarDetail")
    public WebResponse<Boolean> addCarDetail(@Valid @RequestBody ShoppingCartDetailRequest request) {
        return WebResponse.build(cartDetailService.addCarDetail(request));
    }

    @WebAccess(login = true)
    @ApiOperation("购物车商品数量变更")
    @PostMapping("/addCar")
    public WebResponse<Boolean> addCar(@Valid @RequestBody ShoppingCartDetailRequest request) {
        return WebResponse.build(cartDetailService.addCar(request));
    }

    @WebAccess(login = true)
    @ApiOperation("删除购物车商品")
    @PostMapping("/delete")
    public WebResponse<Boolean> deleteByKey(@RequestParam("ids") String ids) {
        return WebResponse.build(cartDetailService.deleteDOByIds(ids));
    }
}
